<?php

namespace app\api\behavior;

use think\Response;

class Verify
{
    const PUBLIC_KEY = '-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCLBy9BLmpbvJkT86A3Fp7lyuFU
0JRUq3UvI95BMm9PZLQD5JvaRkzAPq1DEHMrE/SwywsYxAUrS/rbTJyXz8RJ/Mgz
uJCR86eNGe5x8vpAj5+prq+gmEidrQ/gbqwDzAwNVEy4x98oTVzWlvjwn/0T7WwC
ZZWPSpId8X6IRpXsaQIDAQAB
-----END PUBLIC KEY-----';

    const PRIVATE_KEY = '-----BEGIN RSA PRIVATE KEY-----
MIICWgIBAAKBgQCLBy9BLmpbvJkT86A3Fp7lyuFU0JRUq3UvI95BMm9PZLQD5Jva
RkzAPq1DEHMrE/SwywsYxAUrS/rbTJyXz8RJ/MgzuJCR86eNGe5x8vpAj5+prq+g
mEidrQ/gbqwDzAwNVEy4x98oTVzWlvjwn/0T7WwCZZWPSpId8X6IRpXsaQIDAQAB
An9F9yz7deBl9QO/MVvvsp1EslUJL0QhWCghERaeDkOg240RvdAGiQUXD8kiIaUp
M2HGQb3T43y2eAZGrBzM9EJTkSG/nM3YPmxDrU1MJgjfchy5zpQ9oQG2qfmiFbZe
5jVbetUWgH/0wyblrPGJimVexrLArqzosxV9GZO+hydHAkEAwHUzgDbzb0VIM77L
DuiqMqphezcS8IU1jDdT5tvtU3jDeArc3b9zV2cVx4rbWHv2ZhEBi2euNA9ETIpD
ajcRzwJBALjuBwU+jDUILDGQE0i/vnl4FlqbO3D1ZXiQbJ5PBF2Xvw3jVseB0ZVf
abjlNxdI/g5W+M9krFkXgYReOWRbREcCQCw3gSKW/Wz2HPWOgzakq93CGW1mpNpP
sJLLb2Jd6vO/jiE5Va84ujY9f9p9g5GbwWuHwrQVmQdq7oUrSvvPNS8CQQCmno/k
wb5B6eMnpNA7M8fCWdiKOmMMG44ZWscCu98LFLYnJTYC+4yCMT1oQd2pNTLyaJo1
Alfmza78PIkpSrHxAkBReRNOcF47U2Xk9EOeBhiS4IcTEyUbMv5nJ1GoyA8xoMis
jiicDn2RZA7pDQdyK50AzChdPPqwF7oiLISSOSGv
-----END RSA PRIVATE KEY-----';

    public function run(&$params)
    {
        if (request()->host() == 'api.haohuisong.com') {

            $data = request()->post();
            unset($data['sign']);
            $toSign = $this->parameter($data);

            //$sign = $this->getSign($toSign,self::PRIVATE_KEY);
            //var_dump($sign);exit;

            $sign = 'DxpYdXkd26SocssY0Pa2eQ6IPkiF0QP9GfmtlU5dEWqPtzl2MnIq15exwZVthUzFYuuEFYDsG33sfRpOQPAJEZQAdTZPRHlQ2KR6apB+uF/vwsVKQ22fWNL19dBOa0u9urzYNMb2OXt2RN1hT6r806KrC8aaxCGfI7WG5Yf6/wc=';

            $res = $this->verifySign($toSign, $sign, self::PUBLIC_KEY);

            if (!$res) {
                echo 'Verify Sign Fail';
                exit();
            }
        }

    }

    /**
     * 拼接parameter
     * @param $data
     * @return string
     */
    protected function parameter($data){
        ksort($data);
        $parameter = '';

        foreach ($data as $k => $v) {
            if($parameter){
                $parameter .= '&'.$k.'='.$v ;
            }else{
                $parameter = $k.'='.$v ;
            }
        }

        return $parameter;
    }

    //生成 sha1WithRSA 签名
    public function getSign($toSign, $privateKey){
        /*$privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" .
            wordwrap($privateKey, 64, "\n", true) .
            "\n-----END RSA PRIVATE KEY-----";*/

        $key = openssl_get_privatekey($privateKey);
        openssl_sign($toSign, $signature, $key);
        openssl_free_key($key);
        $sign = base64_encode($signature);
        return $sign;
    }

    //校验 sha1WithRSA 签名
    function verifySign($data, $sign, $pubKey){
        $sign = base64_decode($sign);

        /*$pubKey = "-----BEGIN PUBLIC KEY-----\n" .
            wordwrap($pubKey, 64, "\n", true) .
            "\n-----END PUBLIC KEY-----";*/

        $key = openssl_pkey_get_public($pubKey);
        $result = openssl_verify($data, $sign, $key, OPENSSL_ALGO_SHA1) === 1;
        return $result;
    }
}